package com.chixing.day11_exception.homework;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class Date1 {
    public static void main(String[] args) {
//        1. 获得当前时间
        LocalDate today = LocalDate.now();
        System.out.println(today);
//        2. 获得2天以后的日期
        LocalDate twoDays = today.plus(2, ChronoUnit.DAYS);
        System.out.println(twoDays);
//        3. 获得3天前的日期
        LocalDate threeDays = today.minus(3, ChronoUnit.DAYS);
        System.out.println(threeDays);

//        4. 将字符串"2022/7/21 19:30" 转换成日期对象
        String dateStr = "2022/7/21 19:30";
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        try {
            Date date3 =  dateFormat1.parse(dateStr);
            System.out.println(date3);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        5. 比较两天日期对象date  相差的天数
        String next = "1979-06-04";
        String next1 = "2001-09-09";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Date parse1 = simpleDateFormat.parse(next);
            Date parse = simpleDateFormat.parse(next1);
            long Maxtime =(parse.getTime()>parse1.getTime())?parse.getTime():parse1.getTime();
            long Mintime = (parse.getTime()>parse1.getTime())?parse1.getTime():parse.getTime();
            long l = Maxtime - Mintime;
            long l1 =l/(1000*60*60*24);
            System.out.println(next+"和"+next1+"相差了"+l1+"天");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
